package com.mb.app.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.domain.TbCouponCfg;
import com.mb.app.domain.TbCoupons;
import com.mb.app.dao.TbCouponsMapper;

import com.mb.app.service.ITbCouponCfgService;
import com.mb.app.service.ITbCouponsService;
import com.mb.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 优惠劵信息（用户已领取）  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-05-20
 */
@Service
@Slf4j
public class TbCouponsServiceImpl extends ServiceImpl<TbCouponsMapper, TbCoupons> implements ITbCouponsService {

    @Autowired
    private ITbCouponCfgService couponCfgService;




    @Override
    public List<TbCoupons> queryList(Map<String, Object> map) {
        List<TbCoupons> list = this.baseMapper.list(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.count(map);
    }

    @Override
    public int remove(Long couponId,Long delUserId) {
        TbCoupons TbCoupons = new TbCoupons();
        TbCoupons.setCouponId(couponId);
        TbCoupons.setDelTime(new Date());
        TbCoupons.setDataStatus(0);
        TbCoupons.setDelUserId(delUserId);
        return this.baseMapper.updateById(TbCoupons);
    }

    @Override
    public TbCoupons getDtl(Long couponId) {
        TbCoupons TbCoupons = this.baseMapper.selectDtl(couponId);
        return TbCoupons;
    }

    @Override
    public int update(TbCoupons TbCoupons) {
        try {
            TbCoupons TbCoupons1 = new TbCoupons();
            TbCoupons1.setCouponId(TbCoupons.getCouponId());
            TbCoupons1.setDataStatus(1);
            TbCoupons TbCouponsDO = this.baseMapper.selectOne(TbCoupons1);
            if(TbCouponsDO != null){
                if(TbCoupons.getCouponStatus()==0){
                    TbCouponCfg tbCouponCfg=couponCfgService.getDtl(TbCouponsDO.getCouponCfgId());
                    TbCoupons.setExpireBeginDate(new Date());
                    Integer expireType=tbCouponCfg.getExpireType();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    if(expireType==1){
                        //1:长期有效; 2:1周; 3:15天; 4:1个月; 5:2个月; 6:3个月; 7:6个月; 8：1年
                        String end=DateUtils.getAfterDayDate("yyyy-MM-dd HH:mm:ss",36500);
                        TbCoupons.setExpireEndDate(sdf.parse(end));
                    }else if(expireType==2){
                        String end=DateUtils.getAfterDayDate("yyyy-MM-dd HH:mm:ss",7);
                        TbCoupons.setExpireEndDate(sdf.parse(end));
                    }else if(expireType==3){
                        String end=DateUtils.getAfterDayDate("yyyy-MM-dd HH:mm:ss",15);
                        TbCoupons.setExpireEndDate(sdf.parse(end));
                    }else if(expireType==4){
                        String end=DateUtils.getAfterDayDate("yyyy-MM-dd HH:mm:ss",30);
                        TbCoupons.setExpireEndDate(sdf.parse(end));
                    }else if(expireType==5){
                        String end=DateUtils.getAfterDayDate("yyyy-MM-dd HH:mm:ss",60);
                        TbCoupons.setExpireEndDate(sdf.parse(end));
                    }else if(expireType==6){
                        String end=DateUtils.getAfterDayDate("yyyy-MM-dd HH:mm:ss",90);
                        TbCoupons.setExpireEndDate(sdf.parse(end));
                    }else if(expireType==7){
                        String end=DateUtils.getAfterDayDate("yyyy-MM-dd HH:mm:ss",180);
                        TbCoupons.setExpireEndDate(sdf.parse(end));
                    }else if(expireType==8){
                        String end=DateUtils.getAfterDayDate("yyyy-MM-dd HH:mm:ss",365);
                        TbCoupons.setExpireEndDate(sdf.parse(end));
                    }
                }
                this.baseMapper.updateById(TbCoupons);
            }
        } catch (Exception e) {
            log.error("update Exception",e);
        }
        return 0;
    }
}
